package com.两个链表的第一个公共节点;

import java.util.Stack;

public class Solution {
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
       Stack <ListNode> stack1 = new Stack<ListNode>();
       Stack<ListNode> stack2 = new Stack<ListNode>();
       while(pHead1!=null){
           stack1.push(pHead1);
           pHead1=pHead1.next;
       }
        while(pHead2!=null){
            stack2.push(pHead2);
            pHead2=pHead2.next;
        }
        ListNode first=null;
        while(!stack1.isEmpty()&&!stack2.isEmpty()){
            if(stack1.peek()==stack2.peek()){
                first = stack1.pop();
                stack2.pop();
            }
            else
                return first;
        }
        return null;
    }
}
